﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace genQiang
{
    public partial class Form1 : Form
    {
        public int MV_BEILV = 4; //1个tick移动多少像素;
        public int TICK = 10; //time tick;毫秒
        public int MAX_RANGE = 100;
        public int duojiu = 10;
        public int BOX_SIZE = 75;

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
        }

        int dir = 1;
        int[] dirs=new int[2];
        private void timer1_Tick(object sender, EventArgs e)
        {
            RandomYD();
        }

        private void shuipYD()
        {
            pictureBox1.Left += (dir * 10);
            if (dir == 1 && pictureBox1.Left + pictureBox1.Width > this.Width)
                dir = -1;
            else if (dir == -1 && pictureBox1.Left < 0)
                dir = 1;
        }

        Random random = new Random();
        Queue<int> mvq=new Queue<int>();
        private void RandomYD()
        {
            if (mvq.Count == 0)
            {
                FullDirs();
                //dirs = new int[2] { -1, 100 };
                Trans();
            }
            else
            {
                int mvinfo = mvq.Dequeue();
                if (Math.Abs(mvinfo) > 1) //y轴移动
                {
                    pictureBox1.Top += ((mvinfo >> 1) *MV_BEILV);
                }
                else //x轴移动
                {
                    pictureBox1.Left += (mvinfo*MV_BEILV);
                }
                check();
            }
        }
        private void Trans()
        {
            int x = Math.Abs(dirs[0]);
            int y = Math.Abs(dirs[1]);
            int lim = Math.Max(x, y) / Math.Min(x, y);
            int xp = dirs[0] / Math.Abs(dirs[0]);
            int yp = dirs[1] / Math.Abs(dirs[1]);
            Console.WriteLine("dirs[0]={2},dirs[1]={3} xp={0},yp={1} lim={4}", xp, yp, dirs[0], dirs[1],lim);
            if (x > y)
            {
                while (x != 0)
                {
                    int j = 0;
                    while (x != 0 && j < lim)
                    {
                        mvq.Enqueue(xp);
                        j++;
                        x--;
                    }
                    if (y != 0)
                    {
                        mvq.Enqueue(yp << 1);
                        y--;
                    }
                }
            }
            else
            {
                while (y != 0)
                {
                    int j = 0;
                    while (y != 0 && j < lim)
                    {
                        mvq.Enqueue(yp<<1);
                        j++;
                        y--;
                    }
                    if (x != 0)
                    {
                        mvq.Enqueue(xp);
                        x--;
                    }
                }
            }
            
        }

        private void check()
        {
            if(pictureBox1.Left < 0 ||
                pictureBox1.Left + pictureBox1.Width > this.Width ||
                pictureBox1.Top < 0 ||
                pictureBox1.Top + pictureBox1.Height > this.Height)
            {
                mvq.Clear();
                if (pictureBox1.Left < 0) dirs[0] = random.Next(1, MAX_RANGE);
                else if(pictureBox1.Left + pictureBox1.Width > this.Width ) dirs[0] = random.Next(-MAX_RANGE, -1);
                if (pictureBox1.Top < 0) dirs[1] = random.Next(1, MAX_RANGE);
                else if (pictureBox1.Top + pictureBox1.Height > this.Height) dirs[1] = random.Next(-MAX_RANGE, -1);
                Trans();
            }
        }
        private void FullDirs()
        {
            do
            {
                dirs[0] = random.Next(-MAX_RANGE, MAX_RANGE);
                dirs[1] = random.Next(-MAX_RANGE, MAX_RANGE);
            } while (dirs[0] == 0 || dirs[1] == 0);
        }


        long stime;
        long chu;
        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            long jin = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            stime += (jin-chu);
            chu = 0;
            pictureBox1.BackColor = Color.Green;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            chu = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            pictureBox1.BackColor = Color.Red;
            //Console.WriteLine("hs={0}", chu - jin);
        }

        bool runing = false;
        private void Run(int s)
        {
            SetPanel1.Visible = false;
            this.Focus();
            stime = chu = 0;
            RunContrrolTimer.Interval = s * 1000;
            RunContrrolTimer.Enabled = true;
            timer1.Enabled = true;
            runing = true;
        }


        private void RunContrrolTimer_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            
            //结束，统计

            if(chu!=0)
                stime +=  (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds() - chu);
            Console.WriteLine("t={0}", stime);
            RunContrrolTimer.Enabled = false;
            runing = false;
            MessageBox.Show("跟踪率"+ (100*(1-(stime / (1000.0*duojiu)))).ToString() + "%","oVer");
            initPicBox();
        }

        private void initPicBox()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            pictureBox1.BackColor = Color.Red;
            pictureBox1.Width = pictureBox1.Height = BOX_SIZE;
            pictureBox1.Left=this.Width/2-pictureBox1.Width/2;
            pictureBox1.Top = this.Height / 2 - pictureBox1.Height / 2;
            timer1.Interval = TICK;
            SetPanel1.Visible = false;
            this.Focus();

        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (!runing) Run(duojiu);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initPicBox();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if('Q'==e.KeyChar || 'q' == e.KeyChar)
            {
                this.Close();
                MainWin.mainWin.Visible=true;
            }
        }

        private void 说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PS;\r\n使用鼠标跟踪目标,测试参数可自己调整\r\n鼠标瞄准目标单击鼠标左键开始运行\r\n在一定时间内保持准星处于目标之内\r\n按Q键退出", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void 运行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (runing)
            {
                MessageBox.Show("请等待当前测试运行完毕再重新设置，或者按Q退出重新进行测试!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SetPanel1.Visible = true;
            targetSizeBox.Text = BOX_SIZE.ToString();
            speedBox.Text = MV_BEILV.ToString();
            timeBox.Text = duojiu.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                int ts = int.Parse(targetSizeBox.Text);
                int movespeed = int.Parse(speedBox.Text);
                int rt = int.Parse(timeBox.Text);

                if(ts<10 || ts > 200)
                {
                    MessageBox.Show("目标大小在10-200 PX以内");
                    targetSizeBox.Text = "";
                    return;
                }

                if (movespeed < 1 || movespeed > 10)
                {
                    MessageBox.Show("移动速度在1-10 PX/TICK以内");
                    speedBox.Text = "";
                    return;
                }

                if (rt < 10 || rt > 300)
                {
                    MessageBox.Show("运行时间在10-300 S以内");
                    timeBox.Text = "";
                    return;
                }

                BOX_SIZE = ts;
                MV_BEILV = movespeed;
                duojiu = rt;
                initPicBox();
            }
            catch(Exception expx)
            {
                MessageBox.Show("请输入正确的数字!","Err",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetPanel1.Visible = false;
            this.Focus();
        }

    }
}
